import Koa from 'koa';
import Router from '@koa/router';
import bodyParser from 'koa-bodyparser';
import cors from '@koa/cors';

import userRoutes from './routes/users.js';
import petRoutes from './routes/pets.js';

const app = new Koa();
const router = new Router();

// Middleware
app.use(bodyParser());
app.use(cors({
  origin: '*', // 允许所有来源
  allowMethods: ['GET', 'POST', 'PUT', 'DELETE'], // 允许的 HTTP 方法
  allowHeaders: ['Content-Type', 'Authorization'], // 允许的请求头
}));

// Routes
router.use('/api/users', userRoutes.routes(), userRoutes.allowedMethods());
router.use('/api/pets', petRoutes.routes(), petRoutes.allowedMethods());

// Apply routes
app.use(router.routes()).use(router.allowedMethods());

// Start the server
const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
});
